$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'csg/clinepit/list?state=isdelno&mCGuid='+GetUrlParam("guid"),
        datatype: "json",
        colModel: [
            { label: 'guid', name: 'guid', index: 'guid', width: 50, key: true,hidden:true },
            { label: '项目名称', name: 'pGuid', index: 'p_guid', width: 50,hidden:true },
            { label: '所属隐蔽工程ID', name: 'mCGuid', index: 'm_c_guid', width: 80,hidden:true },
            { label: '照片', name: 'bpicture', index: 'b_picture', width: 80,align: "center",hidden:true,
                sortable: false, editable: false,formatter: alarmFormatter },
            { label: '隐蔽工程类别', name: 'mCName', index: 'm_c_name', width: 80 },
            { label: '设计坑深', name: 'lPDesigndepth', index: 'l_p_designdepth', width: 80 },
            { label: '实际坑深', name: 'lPActualdepth', index: 'l_p_actualdepth', width: 80 },
            { label: '土层深度', name: 'lPSoil', index: 'l_p_soil', width: 80 },
            { label: '砂石深度', name: 'lPStone', index: 'l_p_stone', width: 80 },
            { label: '检查结果', name: 'lPResult', index: 'l_p_result', width: 80 },
            { label: '记录时间', name: 'lPAddtime', index: 'l_p_addtime', width: 80,
                formatter:function(value,options,row){
                    return value!=null?new Date(value).Format('yyyy-MM-dd'):"";
                }
            },
            { label: '状态', name: 'lPDatastate', index: 'l_p_datastate', width: 80,hidden:true }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){

            $('#jqGrid img').click(function (e) {
                var _this = $(this);//将当前的pimg元素作为_this传入函数
                imgShow("#outerdiv", "#innerdiv", "#bigimg", _this);
            });

            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

function alarmFormatter(cellvalue, options, rowdata) {
    return ' <img  src="' +baseURL+'rest/photo/queryPic?address='+ cellvalue + '"  id="img' + rowdata.guid + '"  style="width:40px;height:40px;" />';
}
function imgShow(outerdiv, innerdiv, bigimg, _this){
    var src = _this.attr("src");//获取当前点击的pimg元素中的src属性
    $(bigimg).attr("src", src);//设置#bigimg元素的src属性

	/*获取当前点击图片的真实大小，并显示弹出层及大图*/
    $("<img/>").attr("src", src).load(function(){
        var windowW = $(window).width();//获取当前窗口宽度
        var windowH = $(window).height();//获取当前窗口高度
        var realWidth = this.width;//获取图片真实宽度
        var realHeight = this.height;//获取图片真实高度
        var imgWidth, imgHeight;
        var scale = 0.8;//缩放尺寸，当图片真实宽度和高度大于窗口宽度和高度时进行缩放

        if(realHeight>windowH*scale) {//判断图片高度
            imgHeight = windowH*scale;//如大于窗口高度，图片高度进行缩放
            imgWidth = imgHeight/realHeight*realWidth;//等比例缩放宽度
            if(imgWidth>windowW*scale) {//如宽度扔大于窗口宽度
                imgWidth = windowW*scale;//再对宽度进行缩放
            }
        } else if(realWidth>windowW*scale) {//如图片高度合适，判断图片宽度
            imgWidth = windowW*scale;//如大于窗口宽度，图片宽度进行缩放
            imgHeight = imgWidth/realWidth*realHeight;//等比例缩放高度
        } else {//如果图片真实高度和宽度都符合要求，高宽不变
            imgWidth = realWidth;
            imgHeight = realHeight;
        }
        $(bigimg).css("width",imgWidth);//以最终的宽度对图片缩放

        var w = (windowW-imgWidth)/2;//计算图片与窗口左边距
        var h = (windowH-imgHeight)/2;//计算图片与窗口上边距
        $(innerdiv).css({"top":h, "left":w});//设置#innerdiv的top和left属性
        $(outerdiv).fadeIn("fast");//淡入显示#outerdiv及.pimg
    });

    $(outerdiv).click(function(){//再次点击淡出消失弹出层
        $(this).fadeOut("fast");
    });
}


var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        cLinePit: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.cLinePit = {};
        },
        update: function (event) {
            var guid = getSelectedRow();
            if(guid == null){
                return ;
            }

            var rowData = $("#jqGrid").jqGrid('getRowData',guid);
            var pguid = rowData.pGuid;
            // $.get(baseURL + "csg/cpbase/select?pname="+pname, function(r) {
            //     pguid=r.returnlist[0].guid;

            $.get(baseURL + "csg/cpbase/info/"+pguid, function(r) {
                if (r.cPBase.pBState == 30) {
                    alert("项目已归档，无法修改记录信息！");
                }else{
                    vm.showList = false;
                    vm.title = "修改";
                    $("#lb").attr("readOnly",true);
                    vm.getInfo(guid)
                }
            });


        },
        saveOrUpdate: function (event) {
            var mCGuid = GetUrlParam("guid");
            var pGuid = GetUrlParam("pGuid");

            var url = vm.cLinePit.guid == null ? "csg/clinepit/saveNew/"+mCGuid+"/"+pGuid : "csg/clinepit/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.cLinePit),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if(guids == null){
                return ;
            }
            var guid=guids[0];
            var rowData = $("#jqGrid").jqGrid('getRowData',guid);
            var pguid = rowData.pGuid;
            // $.get(baseURL + "csg/cpbase/select?pname="+pname, function(r) {
            //     pguid=r.returnlist[0].guid;

            $.get(baseURL + "csg/cpbase/info/"+pguid, function(r) {
                if (r.cPBase.pBState != 30) {
                    confirm('确定要删除选中的记录？', function(){
                        $.ajax({
                            type: "POST",
                            url: baseURL + "csg/clinepit/delete",
                            contentType: "application/json",
                            data: JSON.stringify(guids),
                            success: function(r){
                                if(r.code == 0){
                                    alert('操作成功', function(index){
                                        $("#jqGrid").trigger("reloadGrid");
                                    });
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });
                    });
                }else{
                    alert("项目已归档，无法删除记录信息！")
                }
            })
        },
        getInfo: function(guid){
            $.get(baseURL + "csg/clinepit/info/"+guid, function(r){
                vm.cLinePit = r.cLinePit;
                vm.cLinePit.lPAddtime = r.cLinePit.lPAddtime!=null?new Date(r.cLinePit.lPAddtime).Format("yyyy-MM-dd"):"";
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        }
    }
});